package com.hnao.serialport.utils.sqym;

import android.text.TextUtils;
import android.util.Log;
import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;

public class SerialModule extends WXModule {
    private static String TAG = "SerialModule";
    private static long lastCardTime = 0L;
    private JSCallback cardCallback;

    public SerialModule() {
    }

    @JSMethod(
            uiThread = false
    )
    public void open(JSONObject options, JSCallback callback) {
        Log.d(TAG, "open--" + options);
        JSONObject callData = new JSONObject();

        try {
            String path = options.getString("path");
            Integer baudRate = options.getInteger("baudRate");
            Integer dataStartBit = options.getInteger("dataStartBit");
            Integer dataLength = options.getInteger("dataLength");
            if (TextUtils.isEmpty(path)) {
                throw new RuntimeException("请输入path参数");
            }

            if (null == baudRate) {
                throw new RuntimeException("请输入baudRate参数");
            }

            if (null == dataStartBit) {
                throw new RuntimeException("请输入dataStartBit参数");
            }

            if (null == dataLength) {
                throw new RuntimeException("请输入dataLength参数");
            }

            CardReaderHelper.getInstance().initCardReader(path, baudRate, dataStartBit, dataLength);
            callData.put("status", true);
            callData.put("message", "初始化完成");
        } catch (Throwable var8) {
            Throwable e = var8;
            Log.d(TAG, e.getMessage());
            callData.put("status", false);
            callData.put("message", e.getMessage());
        }

        if (null != callback) {
            callback.invoke(callData);
        }

    }

    @JSMethod(
            uiThread = false
    )
    public void close(JSCallback callback) {
        Log.d(TAG, "close--" + callback);
        JSONObject callData = new JSONObject();

        try {
            CardReaderHelper.getInstance().colsePort();
            callData.put("status", true);
            callData.put("message", "关闭完成");
        } catch (Throwable var4) {
            Throwable e = var4;
            Log.d(TAG, e.getMessage());
            callData.put("status", false);
            callData.put("message", e.getMessage());
        }

        if (null != callback) {
            callback.invoke(callData);
        }

    }

    @JSMethod(uiThread = false)
    public JSONObject transData(JSONObject options) {
        JSONObject data = new JSONObject();
        data.put("code", "success");
        String transType = options.getString("transType");
        String cardData = options.getString("cardData");

        try {
            if ("hex2utf8Str".equals(transType)) {
                data.put("cardData", CardNoTransUtil.transHex2Byte2U8Str(cardData));
            } else if ("8H10D+".equals(transType)) {
                data.put("cardData", CardNoTransUtil.trans8H10D(cardData));
            } else if ("8H10D-".equals(transType)) {
                data.put("cardData", CardNoTransUtil.trans8H10D(cardData, false));
            }
        } catch (Exception var6) {
            Exception e = var6;
            data.put("code", "failed");
            data.put("msg", e.getMessage());
        }

        return data;
    }

    public static byte[] hexToByte(String hex) {
        int byteLen = hex.length() / 2;
        byte[] ret = new byte[byteLen];

        for(int i = 0; i < byteLen; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }

        return ret;
    }

    @JSMethod(uiThread = false)
    public void registHandler(JSCallback callback) {
        Log.d(TAG, "registHandler--" + callback);
        this.cardCallback = callback;
        CardReaderHelper.getInstance().setCardReaderListener(new CardReaderHelper.CardReaderListener() {
            public void onCardReaderListener(String cardId) {
                if (System.currentTimeMillis() - lastCardTime > 1000L) {
                    lastCardTime = System.currentTimeMillis();
                    JSONObject callData = new JSONObject();
                    callData.put("card", cardId);
                    cardCallback.invokeAndKeepAlive(callData);
                } else {
                    Log.d(TAG, "刷卡过于频繁");
                }

            }
        });
    }
}
